<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计息开始" prop="rateDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.rateDateStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择计息开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计息结束" prop="costDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.costDateEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择计息结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >分配利息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="interestShareList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应分利息" align="center" prop="shouldMoney" />
      <el-table-column label="实分利息" align="center" prop="rateMoney" />
      <el-table-column label="利息差额" align="center" prop="subMoney" />
      <el-table-column label="结息日" align="center" prop="rateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费截至日期" align="center" prop="costDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleSelect(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改利息分摊对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分摊金额" prop="rateMoney">
          <el-input v-model="form.rateMoney" placeholder="请输入分摊金额"   clearable :style="{width: '100%'}"/>
        </el-form-item>
        <el-form-item label="计息日期" prop="rateDate">
          <el-date-picker  clearable :style="{width: '100%'}"
            v-model="form.rateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择计息日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费日期" prop="costDate">
          <el-date-picker  clearable :style="{width: '100%'}"
            v-model="form.costDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择缴费截至日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<el-form :model="queryParams1" :inline="true" v-show="showSearch" label-width="68px">
    <el-dialog :title="titleDetail" :visible.sync="openDetail" width="1000px" append-to-body>

        <el-table v-loading="loading" :data="interestShareDetailList" >
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column label="实分利息" align="center" prop="rateMoney" />
          <el-table-column label="结息日" align="center" prop="rateDate" />
          <el-table-column label="计息方式" align="center" prop="rateType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.HOUSE004" :value="scope.row.rateType"/>
            </template>
          </el-table-column>
        </el-table>
        <pagination
              v-show="totalDetail>0"
              :total="totalDetail"
              :page.sync="queryParams1.pageNum"
              :limit.sync="queryParams1.pageSize"
              @pagination="getRateDetail"
            />

    </el-dialog>
     </el-form>


  </div>
</template>

<script>
import { listInterestShare, getInterestShare, delInterestShare, addInterestShare, updateInterestShare,listRateDetail } from "@/api/ibatis/interestShare";

export default {
  name: "InterestShare",
  dicts: ['HOUSE004'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalDetail: 0,
      // 利息分摊表格数据
      interestShareList: [],
      interestShareDetailList: [],
      titleDetail: "",
      openDetail: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rateDateStart:null,
        costDateEnd:null,
        rateDate: null,
        costDate: null,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        rateId:null,
        rateDateStart:null,
        costDateEnd:null,
        rateDate: null,
        costDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询利息分摊列表 */
    getList() {
      this.loading = true;
      listInterestShare(this.queryParams).then(response => {
        this.interestShareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rateId: null,
        shouldMoney: null,
        rateMoney: null,
        subMoney: null,
        rateDate: null,
        costDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      debugger
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    handleSelect(row){
        this.openDetail = true
        this.titleDetail = "查看分摊明细";
        this.queryParams1.rateId = row.rateId
        listRateDetail(this.queryParams1).then(response => {
            this.interestShareDetailList = response.rows;
            this.totalDetail = response.total;
            this.loading = false;
          });
        },

        getRateDetail(){
        listRateDetail(this.queryParams1).then(response => {
            this.interestShareDetailList = response.rows;
            this.totalDetail = response.total;
            this.loading = false;
          });
        },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加利息分摊";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rateId = row.rateId || this.ids
      getInterestShare(rateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改利息分摊";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rateId != null) {
            updateInterestShare(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterestShare(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rateIds = row.rateId || this.ids;
      this.$modal.confirm('是否确认删除利息分摊编号为"' + rateIds + '"的数据项？').then(function() {
        return delInterestShare(rateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ibatis/interestShare/export', {
        ...this.queryParams
      }, `interestShare_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
